#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <list>
#include "../../../NetEngine_Linux/NetEngine_SourceCode/XyRyNet_CommHdr.h"
#include "../../../NetEngine_Linux/NetEngine_SourceCode/XyRyNet_Types.h"
#include "../../../NetEngine_Linux/NetEngine_SourceCode/NetEngine_Lib/NetEngine_BaseLib/BaseLib_Define.h"
#include "../../../NetEngine_Linux/NetEngine_SourceCode/NetEngine/NetEngine_WBlackList/WBlackList_Define.h"
#include "../../../NetEngine_Linux/NetEngine_SourceCode/NetEngine/NetEngine_WBlackList/WBlackList_Error.h"

//g++ -std=gnu++11 -Wall -g Test_ForBlackList.cpp -o Test_ForBlackList -L ../../../NetEngine_Linux/NetEngine_Release/NetEngine_Release/NetEngine_BaseLib -L ../../../NetEngine_Linux/NetEngine_Release/NetEngine_Release/NetEngine_Core -lNetEngine_BaseLib -lNetEngine_WBlackList -Wl,-rpath=../../../NetEngine_Linux/NetEngine_Release/NetEngine_Release/NetEngine_BaseLib:../../../NetEngine_Linux/NetEngine_Release/NetEngine_Release/NetEngine_Core,--disable-new-dtags

int Test_IPV4BlackList()
{
    XNETHANDLE xhNet;
    LPCTSTR lpszStartAddr1 = _T("192.168.1.115");
    LPCTSTR lpszStartAddr2 = _T("192.168.2.2");
    LPCTSTR lpszEndAddr2 = _T("192.168.2.115");
    LPCTSTR lpszExist1 = _T("192.168.2.2");
    LPCTSTR lpszExist2 = _T("192.168.2.118");

    if (!WBlackList_IPV4Addr_Init(&xhNet))
    {
        return -1;
    }
    WBlackList_IPV4Addr_Add(xhNet, lpszStartAddr1);
    WBlackList_IPV4Addr_Add(xhNet, lpszStartAddr2, lpszEndAddr2, TRUE);
    WBlackList_IPV4Addr_Add(xhNet, lpszExist1, lpszExist2, TRUE);
    WBlackList_IPV4Addr_Add(xhNet, "192.168.1.1", "192.168.2.255", TRUE);
    WBlackList_IPV4Addr_Add(xhNet, "1.1.1.1", "195.222.2.255", FALSE);
    WBlackList_IPV4Addr_Add(xhNet, "200.1.2.2", "200.1.10.255", TRUE);
    WBlackList_IPV4Addr_Add(xhNet, "200.1.*.2");

    list<BLACKLIST_IPV4ADDR> stl_ListBlack;
    WBlackList_IPV4Addr_GetList(xhNet, &stl_ListBlack);
    list<BLACKLIST_IPV4ADDR>::const_iterator stl_ListIterator = stl_ListBlack.begin();
    for (; stl_ListIterator != stl_ListBlack.end(); stl_ListIterator++)
    {
        printf("%s -> %s\n", stl_ListIterator->tszAddrStart, stl_ListIterator->tszAddrEnd);
    }

    if (WBlackList_IPV4Addr_IsExistBlack(xhNet, "200.1.2.30"))
    {
        printf("找到\n");
    }
    else
    {
        printf("没找到\n");
    }

    WBlackList_IPV4Addr_AddWhite(xhNet, "200.1.2.30");
    if (WBlackList_IPV4Addr_IsExistBlack(xhNet, "200.1.2.30"))
    {
        printf("找到\n");
    }
    else
    {
        printf("没找到\n");
    }
    WBlackList_IPV4Addr_Destory(xhNet);
    return 0;
}
int Test_DomainBlackList()
{
    XNETHANDLE xhNet;
    LPCTSTR lpszDomain1 = _T("http://www.xyry.org");
    LPCTSTR lpszDomain2 = _T("*.*.cctv.org");
    LPCTSTR lpszDomain3 = _T("http://*.cctv.org/1.html");
    LPCTSTR lpszDomain4 = _T("*.*.*.cctv.org");
    LPCTSTR lpszDomain5 = _T("*.app.cctv.org");
    LPCTSTR lpszDomainExist = _T("app.cctv.org");

    if (!WBlackList_Domain_Init(&xhNet))
    {
        return -1;
    }
    WBlackList_Domain_Add(xhNet, lpszDomain1);
    WBlackList_Domain_Add(xhNet, lpszDomain2);
    WBlackList_Domain_Add(xhNet, lpszDomain3);
    WBlackList_Domain_Add(xhNet, lpszDomain4);
    WBlackList_Domain_Add(xhNet, lpszDomain5);

    list<tstring> stl_ListBlack;
    WBlackList_Domain_GetList(xhNet, &stl_ListBlack);
    list<tstring>::const_iterator stl_ListIterator = stl_ListBlack.begin();
    for (; stl_ListIterator != stl_ListBlack.end(); stl_ListIterator++)
    {
        printf("%s\n", stl_ListIterator->c_str());
    }

    if (WBlackList_Domain_IsExistBlack(xhNet, lpszDomainExist))
    {
        printf("找到\n");
    }
    else
    {
        printf("没找到\n");
    }

    WBlackList_Domain_AddWhite(xhNet, lpszDomainExist);
    if (WBlackList_Domain_IsExistBlack(xhNet, lpszDomainExist))
    {
        printf("找到\n");
    }
    else
    {
        printf("没找到\n");
    }
    WBlackList_Domain_Destory(xhNet);
    return 0;
}

int Test_IPV6BlackList()
{
    XNETHANDLE xhNet;
    LPCTSTR lpszStartAddr1 = _T("FF01:0:0:0:1:0:0:1");
    LPCTSTR lpszStartAddr2 = _T("FF01:0:0:0:0:0:2:1");
    LPCTSTR lpszEndAddr2 = _T("FF01:0:0:0:0:0:2:222");
    LPCTSTR lpszExist1 = _T("FF01:0:0:0:0:0:*:1");
    LPCTSTR lpszExist2 = _T("FF01:0:0:0:0:0:3:1101");

    if (!WBlackList_IPV6Addr_Init(&xhNet))
    {
        return -1;
    }
    WBlackList_IPV6Addr_Add(xhNet, lpszStartAddr1);
    WBlackList_IPV6Addr_Add(xhNet, lpszStartAddr2, lpszEndAddr2, FALSE);
    WBlackList_IPV6Addr_Add(xhNet, lpszExist1, lpszExist2, FALSE);
    WBlackList_IPV6Addr_Add(xhNet, "FF01:0:0:0:0:0:0:1", "FF01:0:0:0:0:0:0:FF", FALSE);

    list<BLACKLIST_IPV6ADDR> stl_ListBlack;
    WBlackList_IPV6Addr_GetList(xhNet, &stl_ListBlack);
    list<BLACKLIST_IPV6ADDR>::const_iterator stl_ListIterator = stl_ListBlack.begin();
    for (; stl_ListIterator != stl_ListBlack.end(); stl_ListIterator++)
    {
        printf("%s -> %s\n", stl_ListIterator->tszAddrStart, stl_ListIterator->tszAddrEnd);
    }

    if (WBlackList_IPV6Addr_IsExistBlack(xhNet, lpszStartAddr1))
    {
        printf("找到\n");
    }
    else
    {
        printf("没找到\n");
    }

    WBlackList_IPV6Addr_AddWhite(xhNet, lpszStartAddr1);
    if (WBlackList_IPV6Addr_IsExistBlack(xhNet, lpszStartAddr1))
    {
        printf("找到\n");
    }
    else
    {
        printf("没找到\n");
    }
    WBlackList_IPV6Addr_Destory(xhNet);
    return 0;
}

int main()
{
    Test_IPV4BlackList();
    Test_DomainBlackList();
    Test_IPV6BlackList();

    return 1;
}
